package response

import (
	"sync"
	"wzgames/internal/codec/sfs"
)

var opMsgOPool *sync.Pool

// WebSocket SFS数据包池

func InitRawPacketPool() {
	opMsgOPool = &sync.Pool{
		New: func() interface{} {
			return &sfs.RawPacket{}
		},
	}
}

func AcquireMsgO() *sfs.RawPacket {
	ret := opMsgOPool.Get().(*sfs.RawPacket)
	ret.Pooled = true
	return ret
}

func AcquireMsgO13() *sfs.RawPacket {
	ret := opMsgOPool.Get().(*sfs.RawPacket)
	ret.Id = 13
	ret.ControlId = 1
	ret.Pooled = true
	return ret
}

func ReleaseMsgO(value *sfs.RawPacket) {
	if !value.Pooled {
		return
	}
	if value.Content != nil {
		value.Content = value.Content[:0]
	}
	opMsgOPool.Put(value)
}

func ReleaseRawPacket(msg interface{}) {
	if v, ok := msg.(*sfs.RawPacket); ok {
		ReleaseMsgO(v)
	}
}
